package com.example.demo.service;

import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.Authority;
import com.example.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService implements UserDetailsService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        System.out.println("login:"+s);

        User user = userMapper.findUserByName(s);

        List<Authority>list = new ArrayList<>();
        list.add(userMapper.findAuthorityById(user.getId()));
        user.setAuthorities(list);
        for(Authority authority:list){
            System.out.println(authority.getName());
        }
        return user;
    }
}
